package demo.controller.teacher;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import demo.dao.CourseDao;
import demo.dao.MarkDao;
import demo.dao.TaskDao;
import demo.dao.UserDao;
import demo.entity.Course;
import demo.entity.Mark;
import demo.entity.Task;
import demo.entity.User;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/tea")
public class TeaController {
    @Autowired(required = false)
    UserDao userDao;
    @Autowired(required = false)
    TaskDao taskDao;
    @Autowired(required = false)
    MarkDao markDao;
    @Autowired(required = false)
    CourseDao courseDao;
    @GetMapping("/go2update")
    public String go2Update(Model model, HttpSession session){
        return "/tea/update";
    }
    @PostMapping("/update")
    public String update(User user, Model model, HttpSession session){
        User user1 =(User) session.getAttribute("DL");//拿出用户
        user.setUid(user1.getUid());
        int update = userDao.update(user);//修改密码
        User user2 = userDao.findByUid(user1.getUid());
        session.setAttribute("DL",user2);//设置登录成功标志将用户放在session
        if (update>0) {
            model.addAttribute("update", "修改成功");
        }else  model.addAttribute("update", "修改失败");
        return "/tea/update";
    }
    @RequestMapping("/go2task")
    public String go2task(HttpSession session,Model model){//去显示我的登分和课程
        User dl =(User) session.getAttribute("DL");//获取登录后的用户信息
        List<Task> byTeacher = taskDao.findByTeacher(dl.getUid());//获取数据库教师上课的tesk信息
        model.addAttribute("lists",byTeacher);//将获取的信息传到前端
        return "/tea/task";//跳转到前端页面
    }
    @RequestMapping("/go2mark/{clzno}/{con}")
    public String go2mark(@PathVariable("clzno") String clzno,@PathVariable("con") String cno, HttpSession session, Model model){//去修改或添加成绩拿到班级信息跟课程信息
        List<Mark> clznoCon = markDao.findByClznoCon(clzno,cno);//获取数据库在该班级上课的学生成绩表信息
        model.addAttribute("lists",clznoCon);//将获取的学生成绩表信息传到前端
        return "/tea/mark";
    }
@PostMapping("/add")
    public String add(Model model,Mark mark,HttpSession session)throws  Exception{
    User dl =(User) session.getAttribute("DL");
    if (mark.getId()==0){
        long  time= System.currentTimeMillis();
        Date date=new Date(time);
        mark.setTid(dl.getUid());
        mark.setTpost(date);
        int add = markDao.add(mark);
        model.addAttribute("action","添加成功");
        return "forward:/tea/go2mark/"+mark.getClz().getClzno()+"/"+mark.getCourse().getCon();
    }
    int update = markDao.update(mark);
    model.addAttribute("action","更新成功");
    return "forward:/tea/go2mark/"+mark.getClz().getClzno()+"/"+mark.getCourse().getCon();
}
}
